library AmmoSounds initializer init requires LegacySystem

	private function AgroAmmoSounds takes player p, real dx, real dy returns nothing
		if dx*dx+dy*dy >= 360000.0 then
			call SetPlayerAlliance(Player(13),p,ALLIANCE_PASSIVE,true)
			call SetPlayerAlliance(Player(14),p,ALLIANCE_PASSIVE,true)
			call TriggerSleepAction(0.5)
			call SetPlayerAlliance(Player(13),p,ALLIANCE_PASSIVE,false)
			call SetPlayerAlliance(Player(14),p,ALLIANCE_PASSIVE,false)
		endif
	endfunction
	
	private function AttackSoundsConditions takes nothing returns boolean
		local unit u = GetAttacker()
		local unit target = GetTriggerUnit() // get who gets shot
		local player p
		local integer i = GetUnitTypeId(u)
		local integer id 
		local real dx 
		local real dy
		local integer current
		
		if i == 'H00S' or i=='z004' or i=='z003' or i=='H003' or i=='z00F' or i=='z00G' or i == 'h007' then
			set p = GetOwningPlayer(u)
			set id = GetConvertedPlayerId(p)
			set dx = GetUnitX(u) - GetUnitX(GetTriggerUnit())
			set dy = GetUnitY(u) - GetUnitY(GetTriggerUnit())
			set current = udg_Second

			if (i =='H00S' and udg_Reloading[id]==0) then // sniper
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\xm1014-3.wav"
				call PlaySoundPath3D.execute()
			elseif (i=='H003' and udg_Reloading[id]==0) or ((i=='z00F' or i=='z00G') and GetLoadState(I2S(GetHandleId(u)))==1) then //Marksman
			
				//if(visible(GetUnitX(u), GetUnitY(u), GetUnitX(target), GetUnitY(target), 100.0)==true) then

					if GetUnitAbilityLevel(u,'A04S')>0 or i=='z00G' then
						set udg_Sound3DVolume = 55
						call AgroAmmoSounds.execute(p, dx, dy)
					else
						set udg_Sound3DVolume = 110
					endif
					
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\MarksmanRifle.wav"
					call PlaySoundPath3D.execute()  
				//endif

				set udg_Sound3DVolume = 127
			elseif (i=='H007' and udg_Reloading[id]==0) or ((i=='z004' or i=='z003') and GetLoadState(I2S(GetHandleId(u)))==1) then //Engineer
				if (current-udg_EngyShotLimit!=0) then
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\m3-1.wav"
					call PlaySoundPath3D.execute()
				else
					set udg_EngyShotLimit = current
				endif
			endif
			
		endif
		set u = null
		set p = null
		set target = null
		return false
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_ATTACKED)
		call TriggerAddCondition(t, Condition(function AttackSoundsConditions))
	endfunction




endlibrary